#!/usr/bin/env python
#coding=utf-8

import json
import logging
from pprint import pprint
from aliyunsdkcore.client import AcsClient
from alibabacloud_ims20190815.client import Client as Ims20190815Client
from alibabacloud_tea_openapi import models as open_api_models

from core.logger import logger
from setting import config


class AliYunClient(object):
    def __init__(self):
        pass



    def get_all_client(self):
        aliyun_client = [
            {
                "name": config.SA_ACCOUNT,
                "client": AcsClient(config.SA_ACCESS_KEY, config.SA_ACCESS_SECRET, 'cn-shenzhen')
            },
            {
                "name": config.TIANMIAN_ACCOUNT,
                "client": AcsClient(config.TIANMIAN_ACCESS_KEY, config.TIANMIAN_ACCESS_SECRET, 'cn-shenzhen')
            },
            {
                "name": config.WELABCONSFIN_ACCOUNT,
                "client": AcsClient(config.WELABCONSFIN_ACCESS_KEY, config.WELABCONSFIN_ACCESS_SECRET, 'cn-shenzhen')
            },
            {
                "name": config.WELABFINTECH_ACCOUNT,
                "client": AcsClient(config.WELABFINTECH_ACCESS_KEY, config.WELABFINTECH_ACCESS_SECRET, 'cn-shenzhen')
            },
        ]

        return aliyun_client


    def get_one_client(self, client_name):
        aliyun_client = self.get_all_client()
        for account in aliyun_client:
            if account["name"] == client_name:
                return account

        return None




    # v2 版本使用方式
    def get_all_config_v2(self):
        aliyun_configs = [
            {
                "name": config.SA_ACCOUNT,
                "config": open_api_models.Config(access_key_id=config.SA_ACCESS_KEY, access_key_secret=config.SA_ACCESS_SECRET)
            },
            {
                "name": config.TIANMIAN_ACCOUNT,
                "config": open_api_models.Config(access_key_id=config.TIANMIAN_ACCESS_KEY, access_key_secret=config.TIANMIAN_ACCESS_SECRET)
            },
            {
                "name": config.WELABCONSFIN_ACCOUNT,
                "config": open_api_models.Config(access_key_id=config.WELABCONSFIN_ACCESS_KEY, access_key_secret=config.WELABCONSFIN_ACCESS_SECRET)
            },
            {
                "name": config.WELABFINTECH_ACCOUNT,
                "config": open_api_models.Config(access_key_id=config.WELABFINTECH_ACCESS_KEY, access_key_secret=config.WELABFINTECH_ACCESS_SECRET)
            },
        ]

        return aliyun_configs


    def get_one_client_v2(self, account_name, endpoint):
        aliyun_configs = self.get_all_config_v2()
        for aliyun_config in aliyun_configs:
            if aliyun_config["name"] == account_name:
                aliyun_config["config"].endpoint = endpoint
                return Ims20190815Client(aliyun_config["config"])

        return None




def main():
    aliyun_client = AliYunClient()


if __name__ == "__main__":
    main()